/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * 格式化销售结算单状态 */
function formatOutPaperStatus(value, row) {
    if (value == 0) {   //这里value的类型是String，不能用‘===’三等号
        return '<span style="color:red;">' + '草拟' + '</span>';
    } else if (value == 1) {
        return '<span style="color:green;">' + '待审核' + '</span>';
    } else if (value == 2) {
        return '<span style="color:green;">' + '待审批' + '</span>';
    } else if (value == 3) {
        // return '<span style="color:red;">' + '待实付' + '</span>';
        return '<span style="color:red;">' + '待收款' + '</span>';
    } else if (value == 4) {
        return '<span style="color:green;">' + '已实付' + '</span>';
    } else if (value == 5) {
        // return '<span style="color:red;">' + '已驳回' + '</span>';
        return '<span style="color:red;">' + '草拟' + '</span>';
    } else if (value == 6) {
        return '<span style="color:green;">' + '已提交尾款付款申请' + '</span>';
    } else if (value == 9) {
        return '<span style="color:black;">' + '办结' + '</span>';
    }
}

function formatInPaperStatus(value, row) {
    if (value == 0) {   //这里value的类型是String，不能用‘===’三等号
        return '<span style="color:red;">' + '草拟' + '</span>';
    } else if (value == 1) {
        return '<span style="color:green;">' + '待审核' + '</span>';
    } else if (value == 2) {
        return '<span style="color:green;">' + '待审批' + '</span>';
    } else if (value == 3) {
        // return '<span style="color:red;">' + '待实付' + '</span>';
        return '<span style="color:red;">' + '待付款' + '</span>';
    } else if (value == 4) {
        return '<span style="color:green;">' + '已实付' + '</span>';
    } else if (value == 5) {
        return '<span style="color:red;">' + '已驳回' + '</span>';
    } else if (value == 6) {
        return '<span style="color:green;">' + '已提交尾款付款申请' + '</span>';
    } else if (value == 9) {
        return '<span style="color:black;">' + '办结' + '</span>';
    }
}
function formatProduct(value,row){
    if(value.charAt(0)=='Z'){
        return '废纸';
    }else if(value.charAt(0)=='G'){
        return '废钢';
    }else{
        return '';
    }
}
function checkStatus(value, row) {
    if (value == 0) {   //这里value的类型是String，不能用‘===’三等号
        return '<span style="color:red;">' + '草拟' + '</span>';
    } else if (value == 1) {
        return '<span style="color:green;">' + '待审核' + '</span>';
    } else if (value == 2) {
        return '<span style="color:green;">' + '待审批' + '</span>';
    } else if (value == 3) {
        return '<span style="color:red;">' + '待实付' + '</span>';
    } else if (value == 4) {
        return '<span style="color:green;">' + '已实付' + '</span>';
    } else if (value == 5) {
        return '<span style="color:red;">' + '已驳回' + '</span>';
    } else if (value == 6) {
        return '<span style="color:green;">' + '已提交尾款付款申请' + '</span>';
    } else if (value == 9) {
        return '<span style="color:black;">' + '办结' + '</span>';
    }
}

function payCheckStatus(value, row) {
    if (value == 0) {   //这里value的类型是String，不能用‘===’三等号
        return '<span style="color:red;">' + '草拟' + '</span>';
    } else if (value == 1) {
        return '<span style="color:green;">' + '待审核' + '</span>';
    } else if (value == 2) {
        return '<span style="color:green;">' + '待审批' + '</span>';
    } else if (value == 3) {
        return '<span style="color:red;">' + '待实付' + '</span>';
    } else if (value == 4) {
        return '<span style="color:red;">' + '草拟尾款' + '</span>';
    } else if (value == 5) {
        return '<span style="color:green;">' + '待审核尾款' + '</span>';
    } else if (value == 6) {
        return '<span style="color:green;">' + '待审批尾款' + '</span>';
    } else if (value == 7) {
        return '<span style="color:red;">' + '待实付尾款' + '</span>';
    } else if (value == 9) {
        return '<span style="color:black;">' + '办结' + '</span>';
    }
}

//收款认领状态
function claimStatue(value, row) {
    if (value == 0) {   //这里value的类型是String，不能用‘===’三等号
        return '<span style="color:red;">' + '未认领' + '</span>';
    } else if (value == 2) {
        return '<span style="color:green;">' + '认领' + '</span>';
    }
}

//出库单冲抵状态
function offsetStatue(value, row) {
    if (value == 0) {   //这里value的类型是String，不能用‘===’三等号
        return '<span style="color:red;">' + '未冲抵' + '</span>';
    } else if (value == 3) {
        return '<span style="color:red;">' + '部分冲抵' + '</span>';
    } else if (value == 9) {
        return '<span style="color:red;">' + '全部冲抵' + '</span>';
    }
}

//将日期时间 中的时间截取掉，只剩日期部分 如：2020-12-11 09:09:09 变成2020-12-11
function dateFormat(value, row) {
    if (value) {
        return value.substring(0, 10);
    } else {
        return value;
    }
}
//原始日期格式20210910100101,显示日期格式 yyyy-MM-dd
function dateFormat2(value, row) {
    //var time = row.arrivalTime.subString(0,6);
    //var factoryCode=row.factoryCode;
    if (value != null && value != '') {
        return value.substring(0, 4) + '-' + value.substring(4, 6) + '-' + value.substring(6, 8);
    } else {
        return "";
    }
}
        
function productNameFormatter(value, rowData) {
    let type = rowData.code.substring(0, 1);
    if ('G' === type) {
        return '废钢';
    } else if ('Z' === type) {
        return '废纸';
    }
}

/**
 * 格式化品名*/
function productCodeFormatter(value, rowData) {
    if (value) {
        let type = value.substring(0, 1);
        if ('G' === type) {
            return '废钢';
        } else if ('Z' === type) {
            return '废纸';
        }
    } else {
        return value;
    }
}

function formatUnPayed(value, rowData) {
    if (isNaN(rowData.buyTotalPrice - rowData.buyPayed)) {
        return '0.00';
    } else {
        let unpaid = rowData.buyTotalPrice - rowData.buyPayed;
        return (parseFloat(unpaid).toFixed(2) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
        
    }
}

function formatInvoiceType(value, rowData) {
    if (value == 1001) {
        return '普通发票';
    } else if (value == 1002) {
        return '增值税发票';
    }
}


function formatMoney(value, row, index) {
    if (row != null) {
        if (value) {
            return (parseFloat(value).toFixed(2) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
        } else {
            return "0.00";
        }
    }
}
//计算总价，单价*重量
function formatCalTotalPrice(value, row, index) {
    
    if (row != null) {
        let price = row.price;
        if(!price){
            price = row.unitPrice;
        }
        let weight = row.weight;
        let total=0;
        if(price&&weight){
            total = price*weight;
        }
        if (total) {
            return (parseFloat(total).toFixed(2) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
        } else {
            return "";
        }
    }
}

function formatWeight(value, row, index) {
    if (row != null) {
        if (value) {
            // /(?<!\.\d*)(\d)(?=(\d{3})+($|\.))/g, '$1,'
            // return (parseFloat(value).toFixed(4) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
            return (parseFloat(value).toFixed(4) + '').replace(/(?<!\.\d*)(\d)(?=(\d{3})+($|\.))/g, '$1,');
        } else {
            return "0.0000";
        }
    }
}

function formatAttache(value, rowData) {
    if (isNaN(value)) {
        return '<span style="color:red;">' + '0.00' + '<span>';
    }
    if (value < rowData.amount) {
        return '<span style="color:red;">' + (parseFloat(value).toFixed(2) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,') + '<span>';
    } else {
        return '<span style="color:green;">' + (parseFloat(value).toFixed(2) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,') + '<span>';
    }
}

function anchorFile(value, row) {
    if (value != null && value != '') {
        return "<a target='view_window' href='../../servlet/web/util/ProcessFile?file=" + value + "'>" + "√" + "</a>";  //
    } else {
        return "<font color='red'>" + "×" + "</font>";
    }
}

//图片链接
function anchorPic(value, row) {
    //var time = row.arrivalTime.subString(0,6);
    //var factoryCode=row.factoryCode;
    if (value != null && value != '') {
        //return "<a target='view_window' href='../../photo/"+
        //        time+"/"+factoryCode+"/'+value>"+"√"+"</a>";  //图片链接
        // return "<a target='view_window' href='../common/PictureViewer1.jsp?pic=" + value + "'>" + "√" + "</a>";  //图片链接
        return "<a href='javascript:void(0);' onClick='test1(\""+value+"\")'>" + "√" + "</a>";  //图片链接

    } else {
        return "<font color='red'>" + "×" + "</font>"
    }
}

function formatFile(value, row) {
    //var time = row.arrivalTime.subString(0,6);
    //var factoryCode=row.factoryCode;
    if (value != null && value != '') {
        
        return  "√";
    } else {
        return "<font color='red'>" + "×" + "</font>";
    }
}

function anchorPic0(value, row) {
    //var time = row.arrivalTime.subString(0,6);
    //var factoryCode=row.factoryCode;
    if (value != null && value != '') {
        //return "<a target='view_window' href='../../photo/"+
        //        time+"/"+factoryCode+"/'+value>"+"√"+"</a>";  //图片链接
        // return "<a target='view_window' href='../common/PictureViewer.jsp?pic=" + value + "'>" + "√" + "</a>";  //图片链接
        return "<a href='javascript:void(0);' onClick='test0(\""+value+"\")'>" + "√" + "</a>";  //图片链接
    } else {
        return "<font color='red'>" + "×" + "</font>";
    }
}

var p_pics = new Array();
var p_i = 0;

function anchorPics4Pay(value, row) {
    console.log(p_pics.length);
    // console.log(value.endWith('.pdf'));
    // console.log(value);
    let n = JSON.stringify([]);
    if (value != null && value != '' && value != n) {
        // sessionStorage.setItem('pics4Pay', value);
        //return "<a target='view_window' href='../../photo/"+
        //        time+"/"+factoryCode+"/'+value>"+"√"+"</a>";  //图片链接
        // return "<a target='view_window' href='../common/PictureViewer1.jsp?type=pay'>" + "√" + "</a>";  //图片链接
        p_pics[p_i] = value;
        p_i += 1;
        return "<a href='javascript:void(0);' onClick='showPics(" + value + ")'>" + "√" + "</a>";  //图片链接
    } else {
        return "<font color='red'>" + "×" + "</font>";
    }
}

function showPics(i) {
    console.log(i);
    sessionStorage.setItem('pics4Pay', JSON.stringify(i));
    window.open("../common/PictureViewer1.jsp?type=pay");

}

function anchorPics4Sell(value, row) {
    // console.log(value);
    if (value != null && value != '') {
        sessionStorage.setItem('pics4Sell', value);
        //return "<a target='view_window' href='../../photo/"+
        //        time+"/"+factoryCode+"/'+value>"+"√"+"</a>";  //图片链接
        return "<a target='view_window' href='../common/PictureViewer1.jsp?type=sell'>" + "√" + "</a>";  //图片链接
    } else {
        return "<font color='red'>" + "×" + "</font>";
    }
}

/**
 * 格式化状态显示
 *
 * 格式化状态的显示, 参数一般为
 * value：字段的值。
 * rowData：行的记录数据。
 * rowIndex：行的索引。
 */
function supplierStatus(value, row) {
    /* 状态,  0-表示未审核状态， 1-表示审核通过； 2-表示锁定，9-表示失效 */
    /*if (value === '1') {
        return "启用";
    } else {
        return "停用";
    }*/
    // console.log(value);
    switch (value) {
        case 0:
            return "未审核";
            break;
        case 1:
            return "启用";
            break;
        case 2:
            return "锁定";
            break;
        case 9:
            return "失效";
            break;
        case 90:
            return "已生效";
            break;
        default:
            return "未启用";
    }
}

let img_url = '../../servlet/web/util/ShowPicture?file=';
var img_url0 ='../../photo/';

function test(value, row) {
    if (value != null && value != '') {
        // return `<a> test</a> <br/><a> test</a>`;
        return "<a href='javascript:void(0);' onClick='test1(\""+value+"\")'>" + "√" + "</a>";  //图片链接
    } else {
        return "<font color='red'>" + "×" + "</font>";
    }
}

function test0(value){
    // $('#win_img').attr('src', img_url0 + value);
    // $('#win').window('open');
    $('#win').window('open');
    $('#win_img').attr('src', "").attr('src', img_url0 + value).load(function() {
        // console.log($('#win_img').offset());
        pic_offset_top = $('#win_img').offset().top;
        pic_offset_left = $('#win_img').offset().left;
        pic_real_width = this.width;   // Note: $(this).width() will not
        pic_real_height = this.height; // work for in memory images.
        // console.log(pic_real_width + " x " + pic_real_height);
        $('#win_img').css("width", '95%').css("height", 'auto').offset({top:pic_offset_top, left: pic_offset_left});
    });
}

var pic_real_width, pic_real_height, pic_offset_top, pic_offset_left;

function test1(value){    
    if($('#win_img').length === 0){
        parent.$('#win').window('open');
        parent.$('#win_img').attr('src', "").attr('src', img_url + value).load(function () {
            // console.log(parent.$('#win_img').offset());
            parent.pic_offset_top = parent.$('#win_img').offset().top;
            parent.pic_offset_left = parent.$('#win_img').offset().left;
            console.log(pic_offset_top);
            pic_real_width = this.width;   // Note: $(this).width() will not
            pic_real_height = this.height; // work for in memory images.
            // console.log(pic_real_width + " x " + pic_real_height);
            parent.$('#win_img').css("width", '95%').css("height", 'auto').offset({top:pic_offset_top, left: pic_offset_left});

        });
    }else{
        $('#win').window('open');
        $('#win_img').attr('src', "").attr('src', img_url + value).load(function() {
            // console.log($('#win_img').offset());
            pic_offset_top = $('#win_img').offset().top;
            pic_offset_left = $('#win_img').offset().left;
            console.log(pic_offset_top);
            pic_real_width = this.width;   // Note: $(this).width() will not
            pic_real_height = this.height; // work for in memory images.
            // console.log(pic_real_width + " x " + pic_real_height);
            $('#win_img').css("width", '95%').css("height", 'auto').offset({top:pic_offset_top, left: pic_offset_left});
        });
    }

}

var degree = 0;

function resetRotate(){    
    
    while(degree!=0){
        left90();
    }
    $('#win_img').remove();
    $('#win_div').append("<img id=\"win_img\" src=\"\" width=\"95%\" style=\"margin: 0 auto\" alt=\"加载中......\">");
}

function left90() {
    degree -= 90;
    if (degree < 0)
        degree = degree + 360;
    $('#win_img').css("transform","rotate("+degree+"deg)").offset({top:pic_offset_top, left: pic_offset_left});
    console.log($('#win_img').offset());
}

function right90() {
    degree += 90;
    degree = degree % 360;
    $('#win_img').css("transform","rotate("+degree+"deg)").offset({top:pic_offset_top, left: pic_offset_left});
    console.log($('#win_img').offset());
}

function larger() {
    $('#win_img').attr('width', '190%');
}

function reSize(){
    $('#win_img').attr('width', '95%');
}

